package com.gykfbxxh.imovie.adapter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.utils.util.DateUtils;
import com.app.utils.util.view.image.CircleImageView;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.AppBaseAdapter;
import com.gykfbxxh.imovie.bean.comment.FilmComment;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.utils.glide.ImageLoader;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.List;

import static com.gykfbxxh.imovie.base.BaseActivity.PARAMETER1;

/**
 * 新闻评论适配器
 * Created by xuxinghai on 2016/7/21.
 */
public class FilmCommentAdapter extends AppBaseAdapter<FilmComment> implements View.OnClickListener {

    //逻辑数据
    public int currentLastHotIndex;//当前最后一个热门评论的下标


    public FilmCommentAdapter(List<FilmComment> list, Activity activity) {
        super(list, activity);
    }



    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        FilmComment filmComment = list.get(position);
        if(filmComment == null){
            return 0;
        }
        if(!TextUtils.isEmpty(filmComment.getCommentTypeName())){
            //如果typename不为空则表示 此评论是代码加入的
            return 1;
        }else{
            return 2;
        }
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        int type = getItemViewType(position);
        if(convertView == null){

            if(type == 2){
                convertView = inflater.inflate(R.layout.item_news_comment, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else if(type == 1){
                convertView = inflater.inflate(R.layout.layout_driver_title_margin, null);
                return convertView;
            }else{
                convertView = inflater.inflate(R.layout.item_layout_gone,null);
                return convertView;
            }

        }else {
            if(type == 2){
                viewHolder = (ViewHolder) convertView.getTag();
            }else if(type == 1){
                return convertView;
            }else{
                return convertView;
            }
        }
        FilmComment filmComment = list.get(position);

        if(filmComment == null){
            return convertView;
        }

        ImageLoader.getInstance().loadImage2Cache(context,viewHolder.userImg,filmComment.getfCommentphoto());
        viewHolder.userName.setText(filmComment.getfCommentnick());
//        viewHolder.commentTime.setText(filmComment.getfIntime());
        viewHolder.commentTime.setText(DateUtils.getStrTime_mdhm(DateUtils.getTime(filmComment.getfIntime())));
        viewHolder.message.setText(filmComment.getfContent());
        if(filmComment.getFilm()!=null&&!TextUtils.isEmpty(filmComment.getFilm().getFName())) {
            viewHolder.tv_filmname.setText(filmComment.getFilm().getFName());
            viewHolder.tv_filmname.setVisibility(View.VISIBLE);
            viewHolder.tv_filmname.setTag(R.id.tag_first,filmComment.getFilm());
        }else {
            viewHolder.tv_filmname.setVisibility(View.GONE);
        }

//        //如果评论不显示则 移除该数据
//        if(FilmComment.isHide){
//            list.remove(position);
//            notifyDataSetChanged();
//            if(position >= 0 && position < list.size()){
//                FilmComment = list.get(position);
//            }
//        }
        if(position == currentLastHotIndex){
            viewHolder.lineview.setVisibility(View.INVISIBLE);
        }else if(viewHolder.lineview.getVisibility() == View.INVISIBLE){
            viewHolder.lineview.setVisibility(View.VISIBLE);
        }
        return convertView;
    }



    /*************** 事件 start *************/
    @Override
    public void onClick(View view) {
        if(view == null ){
            return;
        }
        switch (view.getId()) {

            case R.id.comment_username://点击名字
            case R.id.comment_usericon://点击头像

                break;
            case R.id.tv_filmname://点击链接
                FilmEntity film = (FilmEntity) view.getTag(R.id.tag_first);
                Intent intent = new Intent(context, FilmDetailActivity.class);
                intent.putExtra(PARAMETER1 , film.getFId());
                context.startActivity(intent);
                break;
            default:
                break;
        }
    }



    private class ViewHolder {

        CircleImageView userImg;//头像
        TextView userName;//名称
        TextView commentTime;//发布时间
        TextView message;//评论内容
        TextView tv_filmname;
        View lineview;//下划线
        public ViewHolder(View convertView){
            userImg = (CircleImageView) convertView.findViewById(R.id.comment_usericon);
            userName = (TextView) convertView.findViewById(R.id.comment_username);
            commentTime = (TextView) convertView.findViewById(R.id.comment_time);
            message = (TextView) convertView.findViewById(R.id.comment_message);
            lineview = convertView.findViewById(R.id.item_divider);
            tv_filmname = (TextView) convertView.findViewById(R.id.tv_filmname);

            userImg.setOnClickListener(FilmCommentAdapter.this);
            userName.setOnClickListener(FilmCommentAdapter.this);
            tv_filmname.setOnClickListener(FilmCommentAdapter.this);
        }

    }

}

